package com.example.qihuibao.widget.dialog

import android.content.DialogInterface
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.qihuibao.R
import com.example.qihuibao.base.BaseDialogFragment
import com.example.qihuibao.bean.RspQueryCanteenInfoBean
import com.example.qihuibao.bean.RspQueryMealInfoBean
import com.example.qihuibao.widget.view.CommonRecyclerView
import java.lang.StringBuilder


/**
 *@author: cww
 *@desc:
 *@date: 5/16/23 10:58 AM
 */
class MealSelectDialog(private val list:List<RspQueryMealInfoBean>, private val dismissBack:(pos:Int)->Unit) : BaseDialogFragment(R.layout.dialog_list_select) {
    private var index = -1

    override fun initView() {
        mView?.findViewById<TextView>(R.id.tv_title)?.text = "选择餐别"
        val rvCanteen = mView?.findViewById<CommonRecyclerView>(R.id.rv_canteen)
        rvCanteen?.init(
            R.layout.item_meal,
            LinearLayoutManager(context, RecyclerView.VERTICAL, false),
            list,
            {parent, pos ->
                parent.findViewById<TextView>(R.id.tv_meal_name).text = getString(R.string.meal_name, list[pos].MealName)
                parent.findViewById<TextView>(R.id.tv_meal_status).text = getString(R.string.meal_status, if (list[pos].Status=="0") "不开餐" else "开餐")
                parent.findViewById<TextView>(R.id.tv_meal_UseBeginTime).text = getString(R.string.meal_UseBeginTime, list[pos].UseBeginTime)
                parent.findViewById<TextView>(R.id.tv_meal_UseEndTime).text = getString(R.string.meal_UseEndTime, list[pos].UseEndTime)
                parent.findViewById<TextView>(R.id.tv_meal_OrderMode).text = getString(R.string.meal_OrderMode, getOrderMode(list[pos]))
                parent.findViewById<TextView>(R.id.tv_meal_DeliverFlag).text = getString(R.string.meal_DeliverFlag, if (list[pos].DeliverFlag=="0") "否" else "是")
                parent.findViewById<ImageView>(R.id.iv_select).visibility = if (index == pos) View.VISIBLE else View.GONE
            },
            {
                index = it
                rvCanteen?.adapter?.notifyDataSetChanged()
            }
        )
        mView?.findViewById<View>(R.id.btn_ok)?.setOnClickListener { dismiss() }
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        if (index == -1) {
            index = 0
        }
        dismissBack.invoke(index)
    }

    private fun getOrderMode(bean: RspQueryMealInfoBean): String {
        return when(bean.OrderMode){
            "1" -> "套餐"
            "2" -> "点餐"
            "3" -> "简约"
            else -> ""
        }
    }

}